<template>
  <div class="">
    <el-card
      shadow="never"
      :body-style="{ padding: '0px' }"
      :page-size="pageSize"
      :total="rows"
      :current-page="pageNo"
      @current-change="currentChangeHandle"
    >
      <el-table v-loading="dataListLoading" :data="dataList" stripe style="width: 100%">
        <!--        <el-table-column-->
        <!--          prop="appVersionId"-->
        <!--          -->
        <!--          -->
        <!--          min-width="110px"-->
        <!--          :label="$t('appstore.application.appVersionId')">-->
        <!--        </el-table-column>-->
        <el-table-column
          prop="appVersion"
          min-width="110px"
          :label="$t('appstore.application.appVersion')"
        >
        </el-table-column>
        <el-table-column
          prop="submitTime"
          :formatter="formatDate"
          min-width="120px"
          :label="$t('appstore.application.submitTime')"
        >
        </el-table-column>
        <el-table-column
          prop="releaseTime"
          :formatter="formatDate"
          min-width="120px"
          :label="$t('appstore.application.releaseTime')"
        >
        </el-table-column>
        <el-table-column
          prop="releaseType"
          min-width="120px"
          :label="$t('appstore.application.releaseType')"
        >
          <template v-slot="scope">
            <label type="text" v-if="scope.row.releaseType === 1">{{
              $t("appstore.application.releaseType1")
            }}</label>
            <label type="text" v-if="scope.row.releaseType === 2">{{
              $t("appstore.application.releaseType2")
            }}</label>
            <label type="text" v-if="scope.row.releaseType === 3">{{
              $t("appstore.application.releaseType3")
            }}</label>
          </template>
        </el-table-column>
        <el-table-column
          prop="status"
          min-width="140px"
          :label="$t('appstore.application.status')"
        >
          <!--          <template v-slot="scope">-->
          <!--            <label type="text">{{ handleStatus(scope.row.status) }}</label>-->
          <!--          </template>-->
          <template v-slot="scope">
            <el-tag
              v-if="
                scope.row.status === -1 ||
                scope.row.status === 0 ||
                scope.row.status === 2 ||
                scope.row.status === 6
              "
              size="small"
              type="warning"
              >{{ handleStatus(scope.row.status) }}</el-tag
            >
            <el-tag v-if="scope.row.status === 1" size="small" type="success">{{
              handleStatus(scope.row.status)
            }}</el-tag>
            <el-tag
              v-if="
                scope.row.status === 5 || scope.row.status === 7 || scope.row.status === 9
              "
              size="small"
              type="private"
              >{{ handleStatus(scope.row.status) }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column min-width="100px" fixed="right" :label="$t('common.operate')">
          <template v-slot="scope">
            <el-tooltip
              class="item"
              effect="dark"
              :content="$t('common.detail')"
              placement="top"
            >
              <el-button
                text
                class="w-8"
                @click="showDetailHandle(scope.row.appId, scope.row.appVersionId)"
                ><svg-icon icon-class="View"
              /></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageNo"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="rows"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination> -->
    <app-version-detail
      v-if="appVersionDetailVisible"
      ref="appVersionDetail"
      @close="appVersionDetailVisible = false"
      @refreshDataList="refreshData"
    ></app-version-detail>
  </div>
</template>
<script>
import { appVersionList } from "@/api/developer-app";
import appVersionDetail from "./app-version-detail";
import { formatDate } from "@/utils/formatDate";

export default {
  data() {
    return {
      dataList: [],
      appId: 0,
      pageNo: 1,
      pageSize: 10,
      rows: 0,
      appVersionDetailVisible: false,
      dataListLoading: false,
    };
  },
  components: {
    appVersionDetail,
  },
  methods: {
    init(id) {
      this.visible = true;
      this.appId = id;
      this.getAppVersionList();
    },
    refreshData() {
      this.getAppVersionList();
      this.$emit("refreshData");
    },
    handleStatus(status) {
      if (status === 9) {
        return this.$t("developer_app.version_status_option[0].label");
      } else if (status === 7) {
        return this.$t("developer_app.version_status_option[1].label");
      } else if (status === 6) {
        return this.$t("developer_app.version_status_option[2].label");
      } else if (status === 5) {
        return this.$t("developer_app.version_status_option[3].label");
      } else if (status === 2) {
        return this.$t("developer_app.version_status_option[4].label");
      } else if (status === 1) {
        return this.$t("developer_app.version_status_option[5].label");
      } else if (status === 0) {
        return this.$t("developer_app.version_status_option[6].label");
      } else if (status === -1) {
        return this.$t("developer_app.version_status_option[7].label");
      }
    },
    getAppVersionList() {
      let params = {
        pageNo: this.pageNo,
        pageSize: this.pageSize,
      };
      this.dataListLoading = true;
      appVersionList(this.appId, params)
        .then((data) => {
          console.log("versionList", data.data);
          this.dataList = data.data;
          this.rows = data.rows;
          this.dataListLoading = false;
        })
        .catch((e) => {
          this.dataListLoading = false;
          this.$confirmError(e);

          console.log(`err:${JSON.stringify(e)}`);
        });
    },
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageNo = 1;
      this.getAppVersionList();
    },
    currentChangeHandle(val) {
      this.pageNo = val;
      this.getAppVersionList();
    },
    showDetailHandle(appId, appVersionId) {
      this.appVersionDetailVisible = true;
      this.$nextTick(() => {
        this.$refs.appVersionDetail.init(appId, appVersionId);
      });
    },
    formatDate(row, column, cellValue, index) {
      return formatDate(cellValue, "YYYY-MM-DD HH:mm:ss");
    },
  },
};
</script>
<style lang="scss"></style>
